package cn.edu.zime.tjh47;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Class11 extends AppCompatActivity {
    private List<Map<String, Object>> list = new ArrayList<>();
    private ListView lv;
    private Button btn_add;
    private SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class11);
        InitListData();
        lv = findViewById(R.id.lv_list);
        btn_add = findViewById(R.id.btn_add);
        String[] key = {"id", "name", "ph", "class", "img"};
        int[] to = {R.id.tv_id, R.id.tv_name, R.id.tv_ph, R.id.tv_class, R.id.img_he};
        adapter = new SimpleAdapter(this, list, R.layout.class11_item, key, to);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener((adapterView, view, i, l) -> Edit(i));
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Del(i);
                return true;
            }
        });
    }

    private void Del(int index) {
        final Map<String, Object> map = list.get(index);
        new AlertDialog.Builder(this)
                .setTitle("删除信息")
                .setMessage("确定删除关于" + map.get("name") + "的信息")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        list.remove(index);
                        adapter.notifyDataSetChanged();
                    }
                }).setNegativeButton("取消", null)
                .create().show();
    }

    private void Edit(int i) {
        final Map<String, Object> map = list.get(i);
        final View v = getLayoutInflater().inflate(R.layout.class11_dlg, null);
        EditText id = v.findViewById(R.id.et_id);
        EditText name = v.findViewById(R.id.et_name);
        EditText ph = v.findViewById(R.id.et_ph);
        EditText et_class = v.findViewById(R.id.et_class);

        id.setText((String) map.get("id"));
        name.setText((String) map.get("name"));
        ph.setText((String) map.get("ph"));
        et_class.setText((String) map.get("class"));
        new AlertDialog.Builder(this)
                .setTitle("修改")
                .setView(v)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EditText id = v.findViewById(R.id.et_id);
                        EditText name = v.findViewById(R.id.et_name);
                        EditText ph = v.findViewById(R.id.et_ph);
                        EditText et_class = v.findViewById(R.id.et_class);

                        map.put("id", id.getText().toString());
                        map.put("name", name.getText().toString());
                        map.put("ph", ph.getText().toString());
                        map.put("class", et_class.getText().toString());
                        adapter.notifyDataSetChanged();
                    }
                })
                .setNegativeButton("取消", null)
                .create().show();
    }

    private void InitListData() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", "1");
        map.put("name", "童佳豪");
        map.put("ph", "18457589787");
        map.put("class", "物联2302");
        map.put("img", R.drawable.m1);
        list.add(map);
        map = new HashMap<>();
        map.put("id", "2");
        map.put("name", "小查同学");
        map.put("ph", "13800000001");
        map.put("class", "物联2302");
        map.put("img", R.drawable.m2);
        list.add(map);
        map = new HashMap<>();
        map.put("id", "3");
        map.put("name", "王五");
        map.put("ph", "13700000002");
        map.put("class", "物联2301");
        map.put("img", R.drawable.m3);
        list.add(map);
        map = new HashMap<>();
        map.put("id", "4");
        map.put("name", "老六");
        map.put("ph", "18900000003");
        map.put("class", "物联2402");
        map.put("img", R.drawable.m4);
        list.add(map);
    }

    public void add(View view) {
        final View v = getLayoutInflater().inflate(R.layout.class11_dlg, null);
        new AlertDialog.Builder(this)
                .setTitle("添加用户")
                .setView(v)
                .setPositiveButton("确定", (d, i) -> {
                    EditText id = v.findViewById(R.id.et_id);
                    EditText name = v.findViewById(R.id.et_name);
                    EditText ph = v.findViewById(R.id.et_ph);
                    EditText et_class = v.findViewById(R.id.et_class);

                    Map<String, Object> map = new HashMap<>();
                    map.put("id", id.getText().toString());
                    map.put("name", name.getText().toString());
                    map.put("ph", ph.getText().toString());
                    map.put("class", et_class.getText().toString());
                    map.put("img", R.drawable.f1);
                    list.add(map);
                    adapter.notifyDataSetChanged();
                })
                .setNegativeButton("取消", null)
                .create()
                .show();
    }

    public void back(View view) {
        finish();
    }
}